﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebAddressBook
{
    public partial class UpdateDeptInfo : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string deptId = Request.QueryString["deptid"];
                if (!string.IsNullOrEmpty(deptId))
                {
                    InitDeptInfo(deptId);
                }
            }
        }

        private void InitDeptInfo(string deptId)
        {
            Adrr.BLL.Base_Dept baseDeptBll = new Adrr.BLL.Base_Dept();
            Adrr.Model.Base_Dept baseDeptModel = new Adrr.Model.Base_Dept();
            baseDeptModel = baseDeptBll.GetModel(deptId);

            deptName.Value = baseDeptModel.DeptName;
            deptShortName.Value = baseDeptModel.DeptBriefName;
            deptPinYin.Value = baseDeptModel.PinYin;
            isWorkAddress.SelectedIndex = baseDeptModel.IsWorkDress ? 0 : 1;
            phoneNum.Value = baseDeptModel.PhoneNum;
            //end
        }

        protected void BtUpdateDept_Click(object sender, EventArgs e)
        {
            string deptid= Request.QueryString["deptid"];
            if (string.IsNullOrEmpty(deptid))
                return;
            if (!VerifyInput())
            {
                LbMessage.Text = "<div class='box box-error'>更新部门信息失败，带有 * 的字段不能为空！</div>";
                return;
            }
            Adrr.BLL.Base_Dept baseDeptBll = new Adrr.BLL.Base_Dept();
            Adrr.Model.Base_Dept baseDeptModel = new Adrr.Model.Base_Dept();

            //begin 组织model显示
            baseDeptModel.DeptID = deptid;
            baseDeptModel.DeptName = deptName.Value.Trim();
            baseDeptModel.DeptBriefName = deptShortName.Value.Trim();
            baseDeptModel.IsWorkDress = Convert.ToBoolean(isWorkAddress.SelectedValue);
            baseDeptModel.PhoneNum = phoneNum.Value.Trim();
            baseDeptModel.UpdateTime = DateTime.Now;
            baseDeptModel.PinYin = deptPinYin.Value.Trim();

            try
            {
                baseDeptBll.Update(baseDeptModel);
                LbMessage.Text = "<div class='box box-info'>更新部门信息成功！</div>";
            }
            catch (Exception ex)
            {
                LbMessage.Text = "<div class='box box-error'>更新部门信息失败！具体为："+ex.Message+"</div>";
            }
        }

        private bool VerifyInput()
        {
            if (string.IsNullOrEmpty(deptName.Value.Trim()) || string.IsNullOrEmpty(deptShortName.Value.Trim())
                || string.IsNullOrEmpty(deptPinYin.Value.Trim()))
                return false;
            else if (isWorkAddress.SelectedValue == "True")
            {
                if (!string.IsNullOrEmpty(phoneNum.Value.Trim()))
                    return true;
                else
                    return false;
            }
            else
                return true;
        }
    }
}